package com.ruoyi.energy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.energy.domain.TcCar;
import com.ruoyi.energy.domain.TcPark;
import com.ruoyi.energy.domain.vo.ParkCarResponseVO;
import com.ruoyi.energy.domain.vo.TcCarWXVO;
import com.ruoyi.energy.mapper.TcCarMapper;
import com.ruoyi.energy.service.ITcCarService;
import com.ruoyi.energy.service.ITcParkService;
import com.ruoyi.energy.utils.ParkCarUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 内部车辆Service业务层处理
 *
 * @author cjie
 * @date 2021-11-30
 */
@Service
public class TcCarServiceImpl implements ITcCarService {
    @Autowired
    private TcCarMapper tcCarMapper;

    @Autowired
    private ITcParkService parkservice;

    /**
     * 查询内部车辆
     *
     * @param id 内部车辆ID
     * @return 内部车辆
     */
    @Override
    public TcCar selectTcCarById(String id) {
        return tcCarMapper.selectTcCarById(id);
    }

    /**
     * 查询内部车辆列表
     *
     * @param tcCar 内部车辆
     * @return 内部车辆
     */
    @Override
    public List<TcCar> selectTcCarList(TcCar tcCar) {
        return tcCarMapper.selectTcCarList(tcCar);
    }

    /**
     * 新增内部车辆
     *
     * @param tcCar 内部车辆
     * @return 结果
     */
    @Override
    public int insertTcCar(TcCar tcCar) {
        tcCar.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        tcCar.setCreateTime(DateUtils.getNowDate());
        tcCar.setCreateBy(SecurityUtils.getUsername());
        tcCar.setSyncState("未同步");
        return tcCarMapper.insertTcCar(tcCar);
    }

    /**
     * 修改内部车辆
     *
     * @param tcCar 内部车辆
     * @return 结果
     */
    @Override
    public int updateTcCar(TcCar tcCar) {
        tcCar.setSyncState("未同步");
        return tcCarMapper.updateTcCar(tcCar);
    }

    /**
     * 批量删除内部车辆
     *
     * @param ids 需要删除的内部车辆ID
     * @return 结果
     */
    @Override
    public int deleteTcCarByIds(String[] ids) {
        return tcCarMapper.deleteTcCarByIds(ids);
    }

    /**
     * 删除内部车辆信息
     *
     * @param id 内部车辆ID
     * @return 结果
     */
    @Override
    public int deleteTcCarById(String id) {
        return tcCarMapper.deleteTcCarById(id);
    }

    /**
     * 微信查询内部车辆列表
     *
     * @param tcCar 内部车辆
     * @return 内部车辆
     */
    @Override
    public List<TcCarWXVO> selectTcCarWXVOList(TcCarWXVO tcCar) {
        TcCar search = BeanUtil.toBean(tcCar, TcCar.class);
        List<TcCar> tcCars = tcCarMapper.selectTcCarList(search);
        List<TcCarWXVO> results = new ArrayList<>(tcCars.size());
        for (TcCar tcar : tcCars) {
            results.add(BeanUtil.toBean(tcar, TcCarWXVO.class));
        }
        return results;
    }

    @Override
    public AjaxResult syncData(String id) {
        TcCar car = tcCarMapper.selectTcCarById(id);
        //查询car对应的parkID
        TcPark park = parkservice.selectTcParkById(Long.parseLong(car.getId()));
        if (StringUtils.isNotBlank(park.getParkId())) {
            ParkCarResponseVO re = ParkCarUtil.addMonthCar(park.getParkId(), car.getPlateId(), DateUtil.formatDateTime(car.getBeginDate()), DateUtil.formatDateTime(car.getEndDate()), car.getCarownName(), car.getCarownPhone(), car.getAreaName());
            if (re.getRetCode().equals("1")) {
                car.setSyncState("已同步");
                updateTcCar(car);
                return AjaxResult.success();
            } else {
                AjaxResult.error(re.getErrorDes());
            }
        }
        return AjaxResult.error("系统原因同步失败");
    }
}
